<?php
class Addsubject extends Controller {

	private $_subject;
    public $error;
    public $userLog;
    public $subjectEdit;

	public function __construct() {
		parent::__construct();
		$this->_initalize();
	}

	public function __destruct() {
		parent::__destruct();
	}

	public function index() {
		if(isset($this->_get['id'])){
				$this->subjectEdit = $this->subject->getById($this->_get['id']);
				if($this->userLog->level != 1 && $this->userLog->id != $this->subjectEdit->id || is_null($this->subjectEdit)){
					header("Location:index.php");
				}
		}

		 if (isset($this->_post["subject"])) {
		 	$this->_subject = $this->_post["subject"];
		 	$this->model('subject', '', $this->_subject);
		 	if(isset($this->_get['id'])){
		 		$this->subject->id = $this->_get['id'];
		 		$this->error = $this->subject->update();
		 	}
		 	else{
		 		$this->error = $this->subject->add($this->userLog->id);
		 	}
		 }

		$this->template('add_subject');
	}

	private function _initalize() {
		if (isset($this->_session['user'])) {
			$this->userLog = $this->_session['user']['info'];
		} else {
			header('Location:index.php?c=login');
		}

		$this->model('subject', '', array());
	}
}
?>